package com.jiangkang.lottery.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import com.jiangkang.lottery.po.*;
import com.jiangkang.lottery.util.AppPush;
import com.jiangkang.lottery.util.AppPushTest;
import com.jiangkang.lottery.util.HttpsUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Component
@Order(value = 1)
public class LotteryTask implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("========================================="+LocalDateTime.now());
        AppPush appPush = new AppPush();
        AppPushTest appPush2 = new AppPushTest();
        Map<String, String> wanRecord = new HashMap<String, String>();
        Map<String, String> qianRecord = new HashMap<String, String>();
        Map<String, String> baiRecord = new HashMap<String, String>();
        Map<String, String> shiRecord = new HashMap<String, String>();
        Map<String, String> geRecord = new HashMap<String, String>();
        int num = 7;//连击次数
        HttpsUtils httpsUtils = new HttpsUtils();
        String mark = "";
        while (true){
            Thread.sleep(30000);
            Ssff lotteryFFSS = httpsUtils.getLotteryFFSS();
            if (null!=lotteryFFSS && !mark.equals(lotteryFFSS.getLastIssue()) && null!=lotteryFFSS.getLastWinNum()){
                mark = lotteryFFSS.getLastIssue();
                getWinRecord(lotteryFFSS,appPush,wanRecord, num,0,"万位: ");
                getWinRecord(lotteryFFSS,appPush,qianRecord, num,2,"千位: ");
                getWinRecord(lotteryFFSS,appPush,baiRecord, num,4,"百位: ");
                getWinRecord(lotteryFFSS,appPush,shiRecord, num,6,"十位: ");
                getWinRecord(lotteryFFSS,appPush,geRecord, num,8,"个位: ");
            }
            if (null!=lotteryFFSS.getLastWinNum()){

            }
        }
    }

    private void getWinRecord(Ssff lotteryFFSS,AppPush appPush,Map<String, String> wanRecord, int num, int i,String indexName) throws IOException {
        Odd odd = new Odd();
        Even even = new Even();
        Large large = new Large();
        Small small = new Small();

        if (null!=lotteryFFSS){
            String lastWinNum = lotteryFFSS.getLastWinNum();
            if (null!=lastWinNum){
                String[] split = lastWinNum.split("|");
                if (isEven(Integer.parseInt(split[i]))){
                    wanRecord.put("奇","0");
                    wanRecord.put("偶", wanRecord.get("偶") == null ? "0" : String.valueOf(Integer.parseInt(wanRecord.get("偶")) + 1));
                }else {
                    wanRecord.put("偶","0");
                    wanRecord.put("奇", wanRecord.get("奇") == null ? "0" : String.valueOf(Integer.parseInt(wanRecord.get("奇")) + 1));
                }
                if (isLarge(Integer.parseInt(split[i]))){
                    wanRecord.put("小","0");
                    wanRecord.put("大", wanRecord.get("大") == null ? "0" : String.valueOf(Integer.parseInt(wanRecord.get("大")) + 1));
                }else {
                    wanRecord.put("大","0");
                    wanRecord.put("小", wanRecord.get("小") == null ? "0" : String.valueOf(Integer.parseInt(wanRecord.get("小")) + 1));
                }

                if (Integer.parseInt(wanRecord.get("偶")) > num){
                    even.setLastIssue(lotteryFFSS.getLastIssue());
                    even.setIssue(lotteryFFSS.getIssue());
                    even.setNumber(Integer.parseInt(wanRecord.get("偶")));
                    even.setCreateTime(LocalDateTime.now());
                    even.setUpdateTime(LocalDateTime.now());
                    even.setName("偶");
                    Even e1 = even.selectOne(new QueryWrapper<Even>().eq("issue", lotteryFFSS.getIssue()).or().eq("issue", lotteryFFSS.getLastIssue()));
                    Even e2 = even.selectOne(new QueryWrapper<Even>().eq("lastIssue", lotteryFFSS.getLastIssue()));
                    if (null==e1){
                        even.insert();
                    }else{
                        even.update(new QueryWrapper<Even>().eq("issue", lotteryFFSS.getLastIssue()));
                    }

                    String result = appPush.push(indexName + "奇", indexName + "偶: " + wanRecord.get("偶"));
                    if (result.contains("Limit"))
                        appPush.push2(indexName + "奇", indexName + "偶: " + wanRecord.get("偶"));

                }else if(Integer.parseInt(wanRecord.get("奇")) > num){
                    odd.setLastIssue(lotteryFFSS.getLastIssue());
                    odd.setIssue(lotteryFFSS.getIssue());
                    odd.setNumber(Integer.parseInt(wanRecord.get("奇")));
                    odd.setCreateTime(LocalDateTime.now());
                    odd.setUpdateTime(LocalDateTime.now());
                    odd.setName("奇");
                    Odd o1 = odd.selectOne(new QueryWrapper<Odd>().eq("issue", lotteryFFSS.getLastIssue()).or().eq("issue", lotteryFFSS.getIssue()));
                    Odd o2 = odd.selectOne(new QueryWrapper<Odd>().eq("lastIssue", lotteryFFSS.getLastIssue()));
                    if (null==o1){
                        odd.insert();
                    }else {
                        odd.update(new QueryWrapper<Odd>().eq("issue", lotteryFFSS.getLastIssue()));
                    }
                    String result = appPush.push(indexName+"偶",indexName+"奇: "+wanRecord.get("奇"));
                    if (result.contains("Limit"))
                        appPush.push2(indexName+"偶",indexName+"奇: "+wanRecord.get("奇"));
                }

                if (Integer.parseInt(wanRecord.get("大")) > num){
                    large.setLastIssue(lotteryFFSS.getLastIssue());
                    large.setIssue(lotteryFFSS.getIssue());
                    large.setNumber(Integer.parseInt(wanRecord.get("大")));
                    large.setCreateTime(LocalDateTime.now());
                    large.setUpdateTime(LocalDateTime.now());
                    large.setName("大");
                    Large l1 = large.selectOne(new QueryWrapper<Large>().eq("issue", lotteryFFSS.getLastIssue()).or().eq("issue", lotteryFFSS.getIssue()));
                    Large l2 = large.selectOne(new QueryWrapper<Large>().eq("lastIssue", lotteryFFSS.getLastIssue()));
                    if (null==l1){
                        large.insert();
                    }else {
                        large.update(new QueryWrapper<Large>().eq("issue", lotteryFFSS.getLastIssue()));
                    }
                    String result = appPush.push(indexName+"小",indexName+"大: "+wanRecord.get("大"));
                    if (result.contains("Limit"))
                        appPush.push2(indexName+"小",indexName+"大: "+wanRecord.get("大"));
                }else if (Integer.parseInt(wanRecord.get("小")) > num){
                    small.setLastIssue(lotteryFFSS.getLastIssue());
                    small.setIssue(lotteryFFSS.getIssue());
                    small.setNumber(Integer.parseInt(wanRecord.get("小")));
                    small.setCreateTime(LocalDateTime.now());
                    small.setUpdateTime(LocalDateTime.now());
                    small.setName("小");
                    Small s1 = small.selectOne(new QueryWrapper<Small>().eq("issue", lotteryFFSS.getLastIssue()).or().eq("issue", lotteryFFSS.getIssue()));
                    Small s2 = small.selectOne(new QueryWrapper<Small>().eq("lastIssue", lotteryFFSS.getLastIssue()));
                    if (null==s1){
                        small.insert();
                    }else{
                        small.update(new QueryWrapper<Small>().eq("issue", lotteryFFSS.getLastIssue()));
                    }
                    String result = appPush.push(indexName+"大",indexName+"小: "+wanRecord.get("小"));
                    if (result.contains("Limit"))
                        appPush.push2(indexName+"大",indexName+"小: "+wanRecord.get("小"));
                }
            }

        }
    }

    /*public static void main(String[] args) {
        System.out.println(isEven(2));
        isEven(1);
    }*/
    public static boolean isEven(int a){//是偶数
        if((a&1) != 1){
            return true;
        }
        return false;
    }
    public static boolean isLarge(int a){//是大数
        if( a >= 5 ){
            return true;
        }
        return false;
    }
}
